package com.tbit.uqbike.service.business.impl;

import com.tbit.uqbike.object.pojo.InvoiceConfig;
import com.tbit.uqbike.webmanager.dao.core.InvoiceConfigDao;
import com.tbit.uqbike.service.business.InvoiceConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 发票配置信息标(InvoiceConfig)表服务实现类
 *
 * @author shanyong.mo
 * @since 2025-04-02 10:20:07
 */
@Service("invoiceConfigService")
public class InvoiceConfigServiceImpl implements InvoiceConfigService {
    @Resource
    private InvoiceConfigDao invoiceConfigDao;

    /**
     * 通过品牌id查询单条数据
     *
     * @param brandId 品牌id
     * @return 实例对象
     */
    @Override
    public InvoiceConfig queryByBrandId(Integer brandId) {
        return this.invoiceConfigDao.queryByBrandId(brandId);
    }


    /**
     * 通过品牌id删除数据
     *
     * @param brandId 品牌id
     * @return 是否成功
     */
    @Override
    public boolean deleteByBrandId(Integer brandId) {
        return this.invoiceConfigDao.deleteByBrandId(brandId) > 0;
    }

    /**
     * 新增或修改数据（会判断null字段，null字段不插入）
     *
     * @param invoiceConfig 实例对象
     * @return 实例对象
     */
    @Override
    public InvoiceConfig addOrEdit(InvoiceConfig invoiceConfig) {
        //修改
        if (Objects.nonNull(invoiceConfig.getId())){
            this.invoiceConfigDao.updateCheckNull(invoiceConfig);
            return invoiceConfig;
        }
        //如果当前品牌存在发票配置,也是进行更新
        InvoiceConfig oldInvoiceConfig = invoiceConfigDao.queryByBrandId(invoiceConfig.getBrandId());
        if (Objects.nonNull(oldInvoiceConfig)){
            invoiceConfig.setId(oldInvoiceConfig.getId());
            this.invoiceConfigDao.updateCheckNull(invoiceConfig);
            return invoiceConfig;
        }
        //新增
        this.invoiceConfigDao.insertCheckNull(invoiceConfig);
        return invoiceConfig;
    }
}
